package com.wx.controller;

import com.wx.entity.SmbmsBill;
import com.wx.entity.SmbmsProvider;
import com.wx.service.SmbmsBillService;


import com.wx.service.SmbmsProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;


import java.util.List;
import javax.servlet.http.HttpSession;

/**
 * @author wangxiao
 * @since 2023-03-20 13:56:51
 */

@Controller
public class SmbmsBillController {
    @Autowired
    HttpSession httpSession;

    @Autowired
    private SmbmsBillService smbmsBillService;
    @Autowired
    SmbmsProviderService smbmsProviderService;

    @RequestMapping("/jsp/getproviderlist.do")
    @ResponseBody
    public List<SmbmsProvider> getproviderlist() {
        IPage<SmbmsProvider> smbmsProviderIPage = smbmsProviderService.getSmbmsProviderByPage(Page.of(1, 1000), null);
        return smbmsProviderIPage.getRecords();
    }

    @RequestMapping("/jsp/addbill.do")
    public String addSmbmsBill(SmbmsBill smbmsBill) {
        smbmsBill.setCreationDate(LocalDateTime.now());
        smbmsBillService.addSmbmsBill(smbmsBill);
        return "redirect:/jsp/billList.do";
    }

    @RequestMapping("/jsp/billList.do")
    public String smbmsBillList(String pageIndex,String queryProductName,String queryProviderId,String queryIsPayment) {
        int currentPageNo = pageIndex == null ? 1 : Integer.valueOf(pageIndex);
        Page page = Page.of(currentPageNo, 5);
        SmbmsBill smbmsBill = new SmbmsBill();
        smbmsBill.setProductName(queryProductName);
        queryProductName = queryProductName==null?"":queryProductName;
        queryProviderId = queryProviderId==null?"0":queryProviderId;
        queryIsPayment = queryIsPayment==null?"0":queryIsPayment;
        smbmsBill.setProviderId(Integer.valueOf(queryProviderId));
        smbmsBill.setIsPayment(Integer.valueOf(queryIsPayment));
        IPage<SmbmsBill> smbmsBillIPage = smbmsBillService.getSmbmsBillByPage(page, smbmsBill);
        IPage<SmbmsProvider> smbmsProviderIPage = smbmsProviderService.getSmbmsProviderByPage(Page.of(1, 1000), null);
        httpSession.setAttribute("queryProductName", queryProductName);
        httpSession.setAttribute("queryProviderId", smbmsBill.getProviderId());
        httpSession.setAttribute("providerList", smbmsProviderIPage.getRecords());
        httpSession.setAttribute("queryIsPayment", smbmsBill.getIsPayment());
        httpSession.setAttribute("billList", smbmsBillIPage.getRecords());
        httpSession.setAttribute("totalPageCount", smbmsBillIPage.getPages());
        httpSession.setAttribute("totalCount", smbmsBillIPage.getTotal());
        httpSession.setAttribute("currentPageNo", currentPageNo);
        return "billlist";
    }






    @RequestMapping("jsp/updbill.do")
    public String updSmbmsBill(SmbmsBill smbmsBill) {
        smbmsBillService.updSmbmsBill(smbmsBill);
        return "redirect:/jsp/billList.do";
    }


    @RequestMapping("/jsp/delbill.do")
    @ResponseBody
    public HashMap delSmbmsBill(String billid) {
        smbmsBillService.delSmbmsBill(billid);
        HashMap resultMap = new HashMap<>();
        resultMap.put("delResult", "true");
        return resultMap;
    }


    @RequestMapping("/jsp/viewbill.do")
    public String viewSmbmsBill(String method, String billid) {

        SmbmsBill smbmsBill = smbmsBillService.getSmbmsBillById(billid);
        httpSession.setAttribute("bill", smbmsBill);
        if (method.equals("view")) {
            return "billview";
        } else {
            return "billmodify";
        }
    }


}